﻿using FastCodeGeneration.Entity;
using FastCodeGeneration.IRepository;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastCodeGeneration.Repository
{
    public class CodeGeneratorRepository : ICodeGeneratorRepository
    {

        private ConnectionConfig getConnectionConfig(Project project)
        {
            if (project.DataBaseType.ToUpper() == DbType.MySql.ToString().ToUpper())
            {
                MySqlConnectionStringBuilder builder = new MySqlConnectionStringBuilder();
                builder.Database = project.DataBaseName;//数据库名称
                builder.UserID = project.UserName;//用户名
                builder.Password = project.Password;//密码
                builder.Port = project.Port;//端口号码
                builder.Server = project.Ip;//服务器
                var connectionConfig = new ConnectionConfig();
                connectionConfig.ConnectionString = builder.ConnectionString;
                connectionConfig.DbType = DbType.MySql;
                connectionConfig.IsAutoCloseConnection = false;
                return connectionConfig;
            }else if (project.DataBaseType.ToUpper() == DbType.SqlServer.ToString().ToUpper())
            {
                //Server=127.0.0.1;Port=3306;Database=CoreShop;Uid=CoreShop;Pwd=CoreShop;CharSet=utf8;pooling=true;SslMode=None;Allow User Variables=true;Convert Zero Datetime=True;Allow Zero Datetime=True;
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = "mysqldemo";//数据库名称
                builder.UserID = "root";//用户名
                builder.Password = "123456";//密码
                builder.InitialCatalog = "Db";
                var connectionConfig = new ConnectionConfig();
                connectionConfig.ConnectionString = builder.ConnectionString;
                connectionConfig.DbType = DbType.MySql;
                connectionConfig.IsAutoCloseConnection = false;
                return connectionConfig;
            }

            return null;
        }

        /// <summary>
        /// 获取所有的表
        /// </summary>
        /// <returns></returns>
        public List<DbTableInfo> GetDbTables(Project project)
        {
            var db = new SqlSugarClient(getConnectionConfig(project));
            db.Open();
            var tables = db.DbMaintenance.GetTableInfoList(false).OrderBy(p => p.Name).ToList();
            //var views = db.DbMaintenance.GetViewInfoList(false).OrderBy(p => p.Name).ToList();
            //if (!views.Any()) return tables;
            //var newList = tables.Union(views).ToList();
            db.Close();
            db.Dispose();
            return tables;
        }

        /// <summary>
        ///     获取表下面所有的字段
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public List<DbColumnInfo> GetDbTablesColumns(string tableName, Project project)
        {
            var db = new SqlSugarClient(getConnectionConfig(project));
            db.Open();
            var columns = db.DbMaintenance.GetColumnInfosByTableName(tableName, false);
            db.Close();
            db.Dispose();
            return columns;
        }
    }
}
